package com.woniuxy.house.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.entity.house.Newhouse;
import com.woniuxy.entity.house.dto.HouseInfoDto;
import com.woniuxy.entity.house.dto.HouseTypeInfoDto;
import com.woniuxy.entity.house.dto.SelectHouseDto;
import com.woniuxy.house.mapper.BuildingMapper;
import com.woniuxy.house.mapper.NewhouseMapper;
import com.woniuxy.house.service.NewhouseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liangyu
 * @since 2021-03-19
 */
@Service
public class NewhouseServiceImpl extends ServiceImpl<NewhouseMapper, Newhouse> implements NewhouseService {

    @Resource
    private NewhouseMapper newhouseMapper;
    @Resource
    private BuildingMapper buildingMapper;

    /**
     * 通过id查询新房
     * @param id
     * @return
     */
    @Override
    public Newhouse findNewHouseById(int id) {
        return newhouseMapper.selectById(id);
    }

    /**
     * 通过楼盘id查询所有新房
     * @param id
     * @return
     */
    @Override
    public List<Newhouse> findNewHousesByBuildingId(int id) {
        Map<String ,Object> map = new HashMap();
        map.put("bid",id);
        System.out.println("id="+id);
        return newhouseMapper.selectByMap(map);
    }

    /**
     * 新房上架
     * @param newhouse
     * @return
     */
    @Override
    public boolean insertNewhouseToBuilding(Newhouse newhouse) {
        if(newhouseMapper.insert(newhouse)>0){
            //新增房屋成功后，修改对应楼盘在售房屋数量
            if(buildingMapper.updateSaleNum(newhouse.getBid(),1)>0){
                //查询现在均价
                BigDecimal avgprice = newhouseMapper.findAvgById(newhouse.getBid());
                //修改楼盘均价
                if(buildingMapper.updateAvg(newhouse.getBid(),avgprice)>0){
                    //修改均价成功
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 新房下架
     * @param id 新房id
     * @return
     */
    @Override
    public boolean deleteNewHouse(int id) {
        int bid = newhouseMapper.selectById(id).getBid();
        //修改房屋状态
        if(newhouseMapper.updateById(id)>0){
            //修改楼盘在售数量
            if(buildingMapper.updateSaleNum(bid,-1)>0) {
                //查询现在均价
                BigDecimal avgprice = newhouseMapper.findAvgById(bid);
                //修改楼盘均价
                if (buildingMapper.updateAvg(bid,avgprice) > 0) {
                    //修改均价成功
                    return true;
                }
            }
        }
        return false;
    }


    /**
     * 修改新房信息
     * @param newhouse
     * @return
     */
    @Override
    public boolean updateInfo(Newhouse newhouse) {

        UpdateWrapper queryWrapper = new UpdateWrapper();
        queryWrapper.eq("id",newhouse.getId());
        return newhouseMapper.update(newhouse,queryWrapper)>0;
    }

    /**
     * 条件分页查询新房
     *
     * @param current
     * @param size
     * @param selectHouseDto
     * @return
     */
    @Override
    public List<HouseTypeInfoDto> findByCondictions(int current, int size, SelectHouseDto selectHouseDto) {
        Page<HouseTypeInfoDto> page = new Page<>(current, size);
        return newhouseMapper.findByCondictions(page, selectHouseDto);
    }

    /**
     * 修改房屋状态
     *
     * @param id
     * @return
     */
    @Override
    public boolean updateById(int id) {
        return newhouseMapper.updateById(id) > 0;
    }


}
